<?php
class ccdsUser
	extends ccdsBackendPlugin
{
	public $name = "ccdsUser";

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function __construct()
	{
		parent::__construct();

		$this->xajax = ccdsXajax::getInstance();

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "UserOpen", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "UserChangePass", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "UserChangeEmail", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "UserRequestPass", $this->name );


	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function UserOpen() {
		$objResponse = new ccdsResponse();
		


		$query = "SELECT * FROM `user` WHERE user_id='".$_SESSION['CMS']['user_id']."'";		
		$res = $this->ccdSQL->query($query);
		if (!$user = $this->ccdSQL->fetch_assoc($res)) 
		{
			$objResponse->call("CMS.AlertWindow('Es ist ein Fehler aufgetreten'");
			return $objResponse;
		}

		$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/vcard.png";
		$objResponse->loadcommands( $this->tools->addTabManager( 'topTab', "User", "<img src=\"".$icon_src."\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">Meine Daten</span>" ) );

		$objResponse->clsTabManager->addPanel( "TabManagerContent_User", array
		(
			"id" => "UserOverviewTab",
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/vcard.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">&Uuml;bersicht</span>",
			"content" => "<div id='CMS_UserOverview'></div>",
			"closeable" => false
		));		
		
		$this->smarty->assign("user",$user);
		$sHtml = $this->smarty->fetch( $this->tpl_dir."/overview.tpl");
		$objResponse->assign("CMS_UserOverview","innerHTML",$sHtml);
		return $objResponse;
	}
	
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function UserChangePass($aFormValues) {
		$objResponse = new ccdsResponse();

		$query = "SELECT * FROM `user` WHERE user_id='".$_SESSION['CMS']['user_id']."'";		
		$res = $this->ccdSQL->query($query);
		if (!$user = $this->ccdSQL->fetch_assoc($res)) 
		{
			$objResponse->call("CMS.AlertWindow('Es ist ein Fehler aufgetreten'");
			return $objResponse;
		}
		
		if (!isset($aFormValues['pass_old']{3}))
		{
			$objResponse->assign("pass_old_error","innerHTML","Bitte geben Sie Ihr altes Passwort ein!");
			return $objResponse;
		}

		if ( md5($aFormValues['pass_old']) != $user['user_login_pass'] )
		{
			$objResponse->assign("pass_old_error","innerHTML","Das eingegebene Passwort ist falsch!");
			return $objResponse;
		}
		$objResponse->assign("pass_old_error","innerHTML","");

		if (!isset($aFormValues['pass_new']{5}))
		{
			$objResponse->assign("pass_new_error","innerHTML","Das Passwort muss mindest 6 Zeichen haben!");
			return $objResponse;
		}
		$objResponse->assign("pass_new_error","innerHTML","");

		if ($aFormValues['pass_new'] != $aFormValues['pass_new_confirm'])
		{
			$objResponse->assign("pass_new_confirm_error","innerHTML","Die Passw&ouml;rter stimmen nicht überein");
			return $objResponse;
		}
		$objResponse->assign("pass_new_confirm_error","innerHTML","");

		$query = "UPDATE user SET user_login_pass ='".MD5($aFormValues['pass_new'])."' WHERE user_id='".$_SESSION['CMS']['user_id']."' LIMIT 1";
		$this->ccdSQL->query($query);
		
		$objResponse->clear("CMS_pass_new_confirm",'value');
		$objResponse->clear("CMS_pass_new",'value');
		$objResponse->clear("CMS_pass_old",'value');

		$objResponse->clear("pass_old_error",'innerHTML');
		$objResponse->clear("pass_new_error",'innerHTML');
		$objResponse->clear("pass_new_confirm_error",'innerHTML');

		$this->tools->notify($objResponse,"Das Passwort wurfe geändert");
		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function UserChangeEmail($aFormValues) {
		$objResponse = new ccdsResponse();



		if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", trim($aFormValues['user_email']))) 
		{
			$objResponse->assign("user_email_error","innerHTML","Die angegebe E-Mail-Adresse ist ung&uuml;tig!");
			return $objResponse;
		}
		$objResponse->clear("user_email_error","innerHTML");
		
		$query = "UPDATE user SET user_email='".addslashes($aFormValues['user_email'])."' WHERE user_id='".$_SESSION['CMS']['user_id']."' LIMIT 1";
		$this->ccdSQL->query($query);
		

		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function UserRequestPass($aFormValues) {
		$objResponse = new ccdsResponse();

		
		$query = "SELECT * FROM user WHERE user_email='".addslashes(trim($aFormValues['sendpass_mail']))."' LIMIT 1";
		$res = $this->ccdSQL->query($query);
		if (!$row = $this->ccdSQL->fetch_assoc($res)) 
		{
			$objResponse->assign("sendpass_error","innerHTML","Die angegebene E-Mail-Adresse ist nicht bekannt!");
			return $objResponse;
		}
		$objResponse->clear("sendpass_error","innerHTML");
		
		$pass = substr(MD5(time()*rand(0,100)),0,6);
		
		$query = "UPDATE user SET user_login_pass='".md5($pass)."' WHERE user_id='".$row['user_id']."' LIMIT 1";
		$this->ccdSQL->query($query);

		include($this->config->get('LIBRARY_DIR')."htmlMimeMail5/htmlMimeMail5.php");
		
		$from = $this->config->get('ADMIN_MAIL');

		$mail = new htmlMimeMail5();
		$mail->setReturnPath($from);
		$mail->setFrom($from);
		$mail->setSubject("Ihre Zugangsdaten");
		$mail->setHeader('X-Mailer', 'HTML Mime mail class');
	
		$mailtext = "Ihre Zugangsdaten lauten:
		
Benutzer: ".$row['user_login_name']."
Passwort: ".$pass."
";

 		$mail->setText(($mailtext));
		
		$empfaenger = array($aFormValues['sendpass_mail']);
		$result = $mail->send($empfaenger, 'smtp');	
		

		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */
	function getHomeNavi(&$navi) {

		$HN = array(
			"name" => $this->labels['Global_my_account'],
			"onclick" => "xajax.request({ccdsfun:'UserOpen',ccdsclass:'ccdsUser'});"
		);
		$navi[0][] = $HN;
		return true;

	}


}

$ccdsModuleManager = ccdsModuleManager::getInstance();
$ccdsModuleManager->register('home_navi','ccdsuser','getHomeNavi');